package com.yymmarket.forum.chat;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.whirlycott.cache.Cache;
import com.yymmarket.forum.cache.ForumCache;

public class ChatService {

	private static ChatService chatService;

	private ChatDAO chatDAO = new ChatDAO();
	
	private ChatService() {
	}

	public static ChatService getInstance() {
		if (chatService == null) {
			synchronized (ChatService.class) {
				if (chatService == null)
					chatService = new ChatService();
			}
		}
		return chatService;
	}
	
	//public static ConcurrentLinkedHashMap<Integer, ChatBean> chatCache = CachedManager.chatCache;
	public static LinkedList<Integer> chatList = new LinkedList<Integer>();
	public static Cache chatCache2 = ForumCache.chatCache;
	
	
	static{
		ChatDAO chatDAO = new ChatDAO();
		List<ChatBean> list = chatDAO.getAllChats(0, ForumCache.CHAT_MAX_LENGTH);
		for(ChatBean chatBean:list) {
			synchronized(chatList) {
				chatList.addLast(chatBean.getId());
			}
			chatCache2.store(chatBean.getId(), chatBean);
		}
	}
	
	public List<ChatBean> getUserChats(int fromId,int start,int limit) {
		List<Integer> list = this.chatDAO.getUserChats(fromId, start, limit);
		List<ChatBean> chatList = new ArrayList<ChatBean>(limit);
		for(Integer id:list) {
			ChatBean bean = this.getChatBeanById(id);
			if(bean != null) {
				chatList.add(bean);
			}
		}
		return chatList;
	}
	
	public ChatBean getChatBeanById(int id) {
		ChatBean chatBean  = (ChatBean)chatCache2.retrieve(id);
		if (chatBean == null) {
			chatBean = this.chatDAO.getChatBeanById(id);
			if (chatBean != null)
				chatCache2.store(id, chatBean);
		}
		return chatBean;
	}
	
	public int getCountUserChats(int fromId) {
		return this.chatDAO.getCountUserChats(fromId);
	}
	
	
	public List<ChatBean> getAllChats(int start,int limit) {
		List<ChatBean> list = new ArrayList<ChatBean>();
		
		synchronized(chatList) {
			Iterator<Integer> iterator = chatList.iterator();
			int count = 0;
			while(iterator.hasNext()) {
				if(count >= start+limit) {
					break;
				}
				if(count >= start) {
					list.add((ChatBean)(chatCache2.retrieve(iterator.next())));
				}
				count++;
			}
		}
		return list;//this.chatDAO.getAllChats(start, limit);
	}

	public int getAllCount() {
		return chatList.size();
	}
	
	public boolean deleteChat(int id) {
		boolean flag = this.chatDAO.deleteChat(id);
		if(flag) {
			synchronized(chatList) {
				chatList.remove(new Integer(id));
			}
			chatCache2.remove(id);
		}
		return flag;
	}
	
	public boolean addChat(ChatBean bean) {
		boolean flag = this.chatDAO.addChat(bean);
		if(flag) {
			if(bean.getHide()==0) {
				synchronized(chatList) {
					chatList.addFirst(bean.getId());
					while(chatList.size()>ForumCache.CHAT_MAX_LENGTH) {
						chatList.removeLast();
					}
				}
			}
			chatCache2.store(bean.getId(), bean);
		}
		return flag;
	}
	
	public List<ChatBean> getUserChats(int fromId, int toId, int start,int limit){
		List<Integer> list = this.chatDAO.getUserChats(fromId, toId, start, limit);
		List<ChatBean> chatList = new ArrayList<ChatBean>(limit);
		for(Integer id:list) {
			ChatBean bean = this.getChatBeanById(id);
			if(bean != null) {
				chatList.add(bean);
			}
		}
		return chatList;
	}
	
	public int getCountUserChats(int fromId,int toId) {
		return this.chatDAO.getCountUserChats(fromId, toId);
	}
	
}
